<?php

/**
 * This is the model class for table "xd_message_log".
 *
 * @property integer $id
 * @property string $phone
 * @property string $message
 * @property string $send_time
 * @property integer $from
 * @property string $ip
 * @property integer $status
 * @property string $created_time
 */
class Sms
{
    private $apikey;
    const SINGLE = 1;
    
    public function __construct ()
    {
        $this->_messageDao     = new Dao_MessageModel();
    }


    public  function getKey(){
        $apikey = 'c19681c9d923d2169b0a249654dd6f0f';
        return $apikey;
    }
    
    public function singleSend( $mobile, $msg , $ip ){
        $redis = RedisTool::getInstance();
        $apikey = $this->getKey();
        $ttl = $redis->ttl( $mobile.'_code');
        if($ttl > 0 ){
            $code = $redis->get($mobile.'_code');
        }else{
            $code = $this->random(4);
        }
        $msg = str_replace('#code#', $code, $msg);
        // 发送短信
        $data = array(
            'text' => $msg,
            'apikey' => $apikey,
            'mobile' => $mobile
        );
        $url = 'https://sms.yunpian.com/v2/sms/single_send.json';
        $res = $this->curl_post($url,$data);
        if($res['code'] == 0){
            $time = date('Y-m-d H:i:s');
            $now = date('Ymd');
            $redis->set( $mobile.'_code' , $code , 300 );
            $redis->incr( $mobile.'_code_'.$now.'_count');
            $redis->incr( $mobile.'_code_'.$now.'_ip_count');
//            $res = $redis->get( $mobile.'_code' );
            $data = [
                'msg_phone'         => $mobile,
                'msg_message'       => $msg,
                'msg_send_time'     => $time,
                'msg_from'          => 1,
                'msg_ip'            => $ip,
                'msg_status'        => 1,
                'msg_created_time'  => $time,
            ];
            $this->_messageDao->addMessage($data);
        }
        return $res;
    }
    
    private function random($length, $chars = '0123456789') {
        $hash = '';
        $max = strlen($chars) - 1;
        for ($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }
    
    /**
     * curl post
     * @param str $url
     * @param array $data
     * @param array $options
     * @return boolean
     */
    private function curl_post($url, $data = NULL, array $options = array()) {
        if (!$url) {
            return false;
        }
        if (!$data) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data); // 如果为数组，调用http_build_query
        $output = json_decode(curl_exec($ch), true);
        if (curl_errno($ch)) { // 出错则显示错误信息
            return false;
        }
        curl_close($ch);
        return $output;
    }
    
}
